package com.sunsharing.transport;

import com.sunsharing.common.Constant;
import com.sunsharing.common.ContextCreate;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

/**
 * 发送心跳,异常重新连接
 * User: criss
 * Date: 13-4-4
 * Time: 下午3:06
 * To change this template use File | Settings | File Templates.
 */
public class HeartDect implements Runnable {

    Channel channel;
    public HeartDect(Channel ch)
    {
        this.channel = ch;
    }

    Logger logger = Logger.getLogger(HeartDect.class);
    @Override
    public void run() {

            if(channel instanceof LongChannel)
            {
                //长连接发送心跳
                LongChannel c = (LongChannel)channel;
                if(c.isClient())
                {
                    if(c.isclose())
                    {
                        logger.error("断开重新连接:"+channel.url);
                        c.reconnect();
                    }else
                    {
                        //logger.debug("发送心跳报文:"+channel.url);
                        c.send(ContextCreate.createHeartContext());
                    }
                }

//                if((new Date().getTime()-((LongChannel) channel).lastActiveTime)> Constant.HEART_INTERVAL*1000*2)
//                {
//
//                    if(c.isClient())
//                    {
//                        logger.error("超时没有响应重新连接:"+channel.url);
//                        c.reconnect();
//                    }else
//                    {
//                        logger.error("超时没有断开连接:"+channel.url);
//                        c.stop();
//                    }
//                }

            }

    }
}
